﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebsiteClient.Models;
using WebsiteClient.Controllers;
using WebsiteClient.App_Start;
using System.Web;
using Microsoft.AspNet.Identity;
using WebsiteClient.DB;

namespace WebsiteClient.ApiControllers
{
    public class UserController : ApiController
    {
        private static ApplicationDbContext db = new ApplicationDbContext();
        
        // GET: api/User
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/User/5
        public string Get(int id)
        {
            return "value";
        }
        
        
        // POST: api/User
        [HttpPost]
        public void Post(User user)
        {
            PasswordHasher hasher = new PasswordHasher();
            var appUser = new ApplicationUser
            {
                PasswordHash = hasher.HashPassword(user.User_Password),
                Email = user.User_Email,
                UserName = user.User_Email,
                SecurityStamp = Guid.NewGuid().ToString(),
                PhoneNumber = user.User_Phone,
                Name = user.User_Name,
                BirthDate = user.User_Birthdate,
                Address = user.User_Address,
                City = user.User_City,
                Role = 0,
            };

            db.Users.Add(appUser);
            db.SaveChanges();
            
            
        }

        // PUT: api/User/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/User/5
        public void Delete(int id)
        {
        }

        [HttpGet]
        public bool IsEmailAvailable(string email)
        {
            UserDB userDB = new UserDB();

            bool isAvailable = false;

            isAvailable = userDB.IsEmailAvailable(email);

            return isAvailable;
        }
    }
}
